﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace IBS
{

    public class HQCSUPPLIER_CODE
    {
        public Int64 SITECODE { get; set; }
        public Int32 HQC_SHORTCODE { get; set; }
        public Int64 HQC_LONGCODE { get; set; }
    }
    
    
    public class SaleOrderList
    {
        public Int64 INVOICE_ID { get; set; }
    }
        
    public class RPO_DATA : StringEditor
    {
        public Int64 LOCATION_KEY { get; set; } //дополнительно поле, для выгузки не используется
        public Int32 COMPANY { get; set; } //дополнительно поле, для выгузки не используется
        public String IP { get; set; }
        public Int64 INVOICECODE { get; set; }
        public String ORDERCODE { get; set; }
        public Int32 SUPPLIERCODE { get; set; }
        public Int32 STOCKLOCATIONCODE { get; set; }
        public DateTime INVOICEDATE { get; set; }
        public String INVOICETYPE { get; set; }
        public Int32 LINESNUMBER { get; set; }
        public String TOTALQTY { get; set; }
        public Int64 ITEMCODE { get; set; }
        public String INVOICEQUANTITY { get; set; }
        public String SUPPLYQUANTITY { get; set; }
        public String UOM { get; set; }
        public String UOMFACTOR { get; set; }
        public String EMPLOYEECODE { get; set; }
        public String LINEDISCOUNT { get; set; }
        public String INVOICEDISCOUNT { get; set; }
        public String INVOICEEXTRA { get; set; }
        public Double COST { get; set; }
        public String NOTES { get; set; }
        public String ITEMPLATEID { get; set; }
        public String ITEMNAME { get; set; }
        public String ORDERNUMBER { get; set; }
        public String REMARKS { get; set; }
        public String EXTENDEDINVOICENUMBER { get; set; }


        public void SerializeToFile(string filePath)
        {
            String spaceCHR = " ";
            String separator = ",";
            StringBuilder resultString = new StringBuilder();
            resultString.Append(String.Concat(GetFormattedString(INVOICECODE.ToString(), 8, spaceCHR, false), separator));//1
            resultString.Append(String.Concat(GetFormattedString(ORDERCODE, 8, spaceCHR, false), separator));//2
            resultString.Append(String.Concat(GetFormattedString(SUPPLIERCODE.ToString(), 8, spaceCHR, false), separator));//3
            resultString.Append(String.Concat(GetFormattedString(STOCKLOCATIONCODE.ToString(), 8, spaceCHR, false), separator));//4
            resultString.Append(String.Concat(GetFormattedString(INVOICEDATE.ToString("dd.MM.yyyy"), 10, spaceCHR, false), separator));//5
            resultString.Append(String.Concat(GetFormattedString(INVOICETYPE, 2, spaceCHR, false), separator));//6
            resultString.Append(String.Concat(GetFormattedString(LINESNUMBER.ToString(), 8, spaceCHR, false), separator));//7
            resultString.Append(String.Concat(GetFormattedString(TOTALQTY.ToString(), 11, spaceCHR, false), separator));//8
            resultString.Append(String.Concat(GetFormattedString(ITEMCODE.ToString(), 13, spaceCHR, true), separator));//9
            resultString.Append(String.Concat(GetFormattedString(INVOICEQUANTITY.ToString(), 11, spaceCHR, false), separator));//10
            resultString.Append(String.Concat(GetFormattedString(SUPPLYQUANTITY.ToString(), 11, spaceCHR, false), separator));//11
            resultString.Append(String.Concat(GetFormattedString(UOM, 8, spaceCHR, false), separator));//12
            resultString.Append(String.Concat(GetFormattedString(UOMFACTOR, 11, spaceCHR, false), separator));//13
            resultString.Append(String.Concat(GetFormattedString(EMPLOYEECODE, 8, spaceCHR, false), separator));//14
            resultString.Append(String.Concat(GetFormattedString(LINEDISCOUNT, 11, spaceCHR, false), separator));//15
            resultString.Append(String.Concat(GetFormattedString(INVOICEDISCOUNT, 11, spaceCHR, false), separator));//16
            resultString.Append(String.Concat(GetFormattedString(INVOICEEXTRA, 11, spaceCHR, false), separator));//17
            resultString.Append(String.Concat(GetFormattedString(COST.ToString(), 11, spaceCHR, false), separator));//18
            resultString.Append(String.Concat(GetFormattedString(NOTES, 50, spaceCHR, false), separator));//19
            resultString.Append(String.Concat(GetFormattedString(ITEMPLATEID, 8, spaceCHR, false), separator));//20
            resultString.Append(String.Concat(GetFormattedString(ITEMNAME, 20, spaceCHR, false), separator));//21
            resultString.Append(String.Concat(GetFormattedString(ORDERNUMBER, 15, spaceCHR, false), separator));//22
            resultString.Append(String.Concat(GetFormattedString(REMARKS, 40, spaceCHR, false), separator));//23
            resultString.Append(GetFormattedString(EXTENDEDINVOICENUMBER, 20, spaceCHR, true));//24
            File.AppendAllText(filePath, resultString.ToString() + "\r\n");
        }
    }


    public class StringEditor
    {
        public virtual string GetFormattedString(string str, byte lenght, string separator, bool leftAlign)
        {
            if (leftAlign)
            {
                if (String.IsNullOrEmpty(str))
                    return "".PadRight(lenght, Convert.ToChar(separator));
                if (str.Length == lenght)
                    return str;
                if (str.Length > lenght)
                    return str.Remove(lenght);
                if (str.Length < lenght)
                    return str.PadRight(lenght, Convert.ToChar(separator));
            }
            else
            {
                if (String.IsNullOrEmpty(str))
                    return "".PadLeft(lenght, Convert.ToChar(separator));
                if (str.Length == lenght)
                    return str;
                if (str.Length > lenght)
                    return str.Remove(lenght);
                if (str.Length < lenght)
                    return str.PadLeft(lenght, Convert.ToChar(separator));
            }

            return null;
        }
    }

}
